package oa.service.impl;

import java.util.Collection;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import oa.dao.TopicDao;
import oa.domain.forum.Forum;
import oa.domain.forum.Topic;
import oa.service.TopicService;

@Service("topicService")
public class TopicServiceImpl implements TopicService {

	@Resource(name="topicDao")
	private TopicDao topicDao;
	@Override
	public Collection<Topic> findTopicByForum(Forum forum) {
		// TODO Auto-generated method stub
		return this.topicDao.findTopicByForum(forum);
	}
	@Override
	@Transactional(readOnly=false)
	public void saveTopic(Topic topic) {
		// TODO Auto-generated method stub
		this.topicDao.saveEntity(topic); //topic 为持久化对象
		Forum forum = topic.getForum(); //forum也是持久化对象
		forum.setTopicCount(forum.getTopicCount()+1); // 当前主题所在的板块的主题数量加1
		forum.setArticleCount(forum.getArticleCount()+1); //当前主题所在板块的文章数量加1
		forum.setLastTopic(topic); // 设置板块的最后发表的主题为当前的主题
	}
	@Override
	public Topic getTopicById(Long id) {
		// TODO Auto-generated method stub
		return this.topicDao.getEntryById(id);
	}
	@Override
	@Transactional(readOnly=false)
	public void upateTopic(Topic topic) {
		// TODO Auto-generated method stub
		this.topicDao.updateEntry(topic);
	}
	@Override
	@Transactional(readOnly=false)
	public void deleteTopic(Long id) {
		// TODO Auto-generated method stub
		this.topicDao.deleteEntry(id);
	}

}
